<?php
/**
 * Created by PhpStorm.
 * User: a4041
 * Date: 2018/6/13
 * Time: 13:46
 */

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class OrderQueue extends Model
{
    protected $table= 'order_queue';
    public function queueCounts()
    {
        return $this->order()->get();//where('status',0)->get();
    }
    // select * from `orders` where `orders`.`order_id` is null and `orders`.`order_id` is not nul
    public function order(){
        return $this->belongsTo(\App\Models\Order::class,'order_id','id');
    }

    //我的排队订单
    static function getMyQueue($user_id)
    {
        $data = OrderQueue::where('user_id',$user_id)->where('status',0)->get();
        return $data;
    }

    //等待人数
    static function getWaitingPeople($type,$data,$before_time,$after_time)
    {
        //$user_id = Auth::id();
        //桌子等待人数
        $data = OrderQueue::where(function ($query) use ($type) {
            $query->where('number', 'like', '%' . $type . '%')//所选位置
            ->orWhere('number', 'like', '%' . 'C' . '%');//位置不限
        })->where('shop_id', $data['qorder']['shop_id'])//同一家店
        ->where('status', 0)//等候中
        //->where('user_id', '<>', $user_id)//除去自己
        ->where('size', $data['qorder']['size'])//房桌大小
        ->where('created_at', '<', $data['qorder']['created_at'])//下单时间
        ->whereBetween('eat_time',[$before_time,$after_time])//规定1小时用餐
        ->count();
        return $data;
    }

}